<?php

namespace app\admin\model\scanwork;

use think\Model;

class ScheduleTask extends Model
{
    protected $name = 'scanwork_schedule_task';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function schedule()
    {
        return $this->belongsTo('Schedule', 'schedule_id', 'id');
    }

    public function process()
    {
        return $this->belongsTo('Process', 'process_id', 'id');
    }

    public function workCenter()
    {
        return $this->belongsTo('WorkCenter', 'work_center_id', 'id');
    }

    public function equipment()
    {
        return $this->belongsTo('Equipment', 'equipment_id', 'id');
    }

    public function employee()
    {
        return $this->belongsTo('Employee', 'employee_id', 'id');
    }

    public static function generateTaskNo()
    {
        $prefix = 'TASK-';
        $date = date('Ymd');
        
        $lastNo = self::where('task_no', 'like', $prefix . $date . '%')
            ->order('id', 'desc')
            ->value('task_no');
        
        if ($lastNo) {
            $lastNum = intval(substr($lastNo, -4));
            $newNum = str_pad($lastNum + 1, 4, '0', STR_PAD_LEFT);
        } else {
            $newNum = '0001';
        }
        
        return $prefix . $date . $newNum;
    }
}
